<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="order-list">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging"  v-model="dataList" @query="queryList" :fixed="false" :auto="false">
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="list-item" v-for="(item, index) in dataList" :key="index" 
			@click="itemClick(item)">
				<view class="item-header">
					<view class="left">
						{{ item.sellerShopName }}
					</view>
					<view class="right">
						<text :style="{fontSize: '26rpx', color: badgeColor}">{{orderStateText[item.state]}}</text>
					</view>
				</view>
				<view class="u-m-b-30">
					<view class="u-font-24 grey-color">订单号：{{ item.tradeNo }}</view>
					<view>
						<text class="u-font-24 grey-color">付款信息：</text>
						<text class="u-font-24 grey-color" v-if="item.paymentMethod!=null">{{paymentMethod[item.paymentMethod]}}</text>
						<text class="u-font-24 grey-color" v-if="item.paymentMethod!=null&&item.paymentStatus!=null">/</text>
						<text class="u-font-24 grey-color" v-if="item.paymentStatus!=null">{{paymentStatus[item.paymentStatus]}}</text>
					</view>
				</view>
				<view class="item-content" v-for="info in item.orderItems" :key="info.itemId">
					<image :src="info.url" style="width: 100rpx;height:100rpx;border-radius: 10rpx;"></image>
					<view class="u-flex-col u-row-around u-m-l-20" style="flex: 1;overflow: hidden;">
						<view class="u-flex u-row-between">
							<view class="u-font-24 u-line-1">{{ info.itemName }}</view>
							<view class="u-font-24">
								￥{{ info.price }}
							</view>
						</view>
						<view class="u-flex u-row-between">
							<view class="u-flex u-row-left">
								<view class="new-grey-box">
									{{ info.specification }}
								</view>
							</view>
							<view  class="new-grey-box">
								x{{ info.quantity }}
							</view>
						</view>
						
					</view>
				</view>
				<view class="u-text-right">
					<text class="u-font-24 grey-color">总价￥{{ item.amount }}</text>
					<text class="u-font-24 u-m-l-20">实际价 <text class="u-font-24 badge-color">￥{{ item.actualAmount }}</text></text>
				</view>
				<view  v-if="item.state == 0||item.state == 2||item.state == 3">
					<hr style="margin-top: 30rpx;opacity: 0.1;" />
					<view class="item-footer u-flex u-row-right">
						<fui-button text="取消订单" class="u-m-r-20" v-if="item.state == 0" @click="cancelOrderFn(item,'cancel')" height="54rpx" width="150rpx" radius="45rpx" size="24" background="#F8F8F8" color="#999"></fui-button>
						<fui-button text="付款" v-if="item.state == 0"  @click="itemClick(item)"
						height="54rpx" width="150rpx" radius="45rpx" size="24" background="#FFECBF" color="#FFBB1A"></fui-button>
						<fui-button text="确认收货" v-if="item.state == 2"  @click="cancelOrderFn(item,'takeover')"
						height="54rpx" width="150rpx" radius="45rpx" size="24" background="#FFECBF" color="#FFBB1A"></fui-button>
						<fui-button text="评价" v-if="item.state == 3"  @click="itemClick(item)"
						height="54rpx" width="150rpx" radius="45rpx" size="24" background="#FFECBF" color="#FFBB1A"></fui-button>
					</view>
				</view>
				
			</view>
		</z-paging>
	</view>
</template>

<script>
	import {useShopStore} from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		data() {
			return {
				//v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
				modalShow:false,
				dataList: [],
				firstLoaded: false,
				orderStateText:['待确认','待发货','待收货','待评价','已完成','已取消'],
				paymentStatus:['待付款','待到账','已完成','部分完成','支付失败','已关闭','退款中','已退款'],
				paymentMethod:{
					0:'微信支付',
					1:'支付宝支付',
					10:'工行支付',
					3:'线下支付',
					4:'线下支付'
				},
			}
		},
		computed:{
			...mapState(useShopStore, {
				storeShop: 'shop',
			})	
		},
		props:{
			//当前组件的index，也就是当前组件是swiper中的第几个
			tabIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
			//当前swiper切换到第几个index
			currentIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
			value:{
				type: Number,
				default: function(){
					return 0
				}
			},
			keyWords:{
				type: String,
				default: function(){
					return ''
				}
			},
			filterData:{
				type:Object,
				default:function(){
					return {
						startTime:'',
						endTime:'',
						orderNo:'',
						saleShopName:'',
					}
				}
			},
			reload:{
				type: Boolean,
				default: function(){
					return false
				}
			}
			
		},
		watch: {
			currentIndex: {
				handler(newVal) {
					if(newVal === this.tabIndex){
						//懒加载，当滑动到当前的item时，才去加载
						if(this.reload||!this.firstLoaded){
							setTimeout(() => {
								this.$refs.paging.reload();
							}, 100);
						}
					}
				},
				immediate: true
			},
			filterData: {
				handler(newVal) {
					if(this.reload){
						setTimeout(() => {
							this.$refs.paging.reload();
						}, 100);
					}
				},
				deep:true,
				immediate: true
			},
			keyWords:{
				handler(newVal) {
					//懒加载，当滑动到当前的item时，才去加载
					setTimeout(() => {
						this.$refs.paging.reload();
					}, 100);
				},
			}
		},
		methods: {
			reloadData() {
				this.$refs.paging.reload();
			},
			refreshFn() {
				this.$refs.paging.refresh();
			},
			cancelOrderFn(item,type){
				this.$emit('cancelOrder',{id:item.id,type:type})
			},
			async queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					shopId:this.storeShop.id,
					pageIndex: pageNo,
					pageSize: pageSize,
					IsPaging:true,
				}
				if(this.value != 99){
					params.OrderStates = this.value;
				}
				if(this.keyWords!==''){
					params.ItemName = this.keyWords;
				}
				if(this.filterData.saleShopName!==''){
					params.ShopName = this.filterData.saleShopName;
				}
				if(this.filterData.startTime!==''){
					params.StartTime = this.filterData.startTime;
				}
				if(this.filterData.endTime!==''){
					params.EndTime = this.filterData.endTime;
				}
				if(this.filterData.IsPaid!=='all'){
					params.IsPaid = this.filterData.IsPaid
				};
				uni.showLoading({
					title: 'loading...',
					mask: false
				});
				const res = await this.$newHttp.get('api/Trade/GetPurchaseTrades', { params: params } );
				uni.hideLoading({
					title: 'loading...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
					this.firstLoaded = true;
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				this.$mRouter.push({
					route: '/pages/purchase/order/details?id='+item.id,
				})
				// if(item.state != 5){
				// 	this.$mRouter.push({
				// 		route: '/pages/purchase/order/details?id='+item.id,
				// 	})
				// }else{
				// 	uni.$fui.toast('该订单已取消')
				// }
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.order-list{
		height: 100%;
		background-color: #f8f8f8;
		padding:0 30rpx;
		overflow: hidden;
		.list-item:nth-of-type(1){
			margin-top: 30rpx;
		}
	}
	.list-item {
		background-color: #FFFFFF;
		padding: 0 30rpx 20rpx;
		margin-bottom: 20rpx;
		.theme-box {
			border: 1rpx solid $theme-color;
			padding: 5rpx 16rpx;
			border-radius: 5rpx;
		}
		.item-header {
			display: flex;
			justify-content: space-between;
			font-size: 24rpx;
			font-weight: bold;
			line-height: 81rpx;
			height: 81rpx;
			.left {
				font-size: 30rpx;
			}
			.right {
				font-size: 26rpx;
				color: $theme-color;
			}
		}
		.item-content {
			display: flex;
			justify-content: flex-start;
			margin-bottom: 30rpx;
			.center {
				margin-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
			}
		}
		.item-footer {
			padding: 20rpx 0 0;
		}
	}


</style>
